package com.autonavi.yunda.yunji.controller.request;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.controller.validator.annotation.EngineDateValid;
import com.autonavi.yunda.yunji.core.enums.EngineType;
import com.autonavi.yunda.yunji.core.enums.InterfaceBuildType;
import com.autonavi.yunda.yunji.core.enums.InterfaceStatus;
import com.autonavi.yunda.yunji.core.enums.Method;
import com.autonavi.yunda.yunji.core.model.dto.InterfaceConfigDTO;
import com.autonavi.yunda.yunji.core.utils.ValidUtil;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author cizhong.wcs
 * @date 2022/6/27 16:25
 */
@Data
public class InterfaceConfigRequest {
    @NotBlank
    private String name;
    @NotBlank
    private String uri;
    @NotNull
    private Method method;
    @NotBlank
    private String version;
    @NotBlank
    private String projectId;
    private InterfaceBuildType buildType;
    private EngineType engineType;
    @EngineDateValid
    private String engineData;
    private String extraData;
    private String description;

    public InterfaceConfigDTO toAddDTO(String empId) {
        checkUri();
        return InterfaceConfigDTO.builder()
                .name(name)
                .uri(uri)
                .method(method)
                .version(version)
                .projectId(projectId)
                .engineData(engineData)
                .extraData(extraData)
                .createUser(empId)
                .updateUser(empId)
                .status(InterfaceStatus.INIT)
                .description(description)
                .buildType(buildType)
                .engineType(engineType)
                .build();

    }

    public InterfaceConfigDTO toValidatingDTO(String empId) {
        InterfaceConfigDTO dto = toAddDTO(empId);
        dto.setStatus(InterfaceStatus.VALIDATING);
        return dto;
    }

    private void checkUri() {
        AmapPreconditions.checkArgumentForUser(ValidUtil.checkRegexAndReturn(uri, "^(/[\\w-]+)+$"), "uri不符合规范,正则表达式:^(/[\\w-]+)+$");
    }
}
